package org.lyq.erp.auth.employee.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.lyq.erp.auth.employee.mapper.EmployeeMapper;
import org.lyq.erp.auth.employee.pojo.Employee;
import org.lyq.erp.auth.employee.pojo.EmployeeExcelModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 员工模块业务处理类
 * 
 * @ClassName: EmployeeServiceImpl
 * @Description:
 * @author LYQ(廖先生)
 * @date 2017年12月15日 下午9:36:16
 *
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

	@Resource
	private EmployeeMapper employeeMapper;

	@Transactional
	public void insertSelective(Employee employee) {
		employeeMapper.insertSelective(employee);
	}

	public Employee selectByPrimaryKey(Integer empUuid) {

		return employeeMapper.selectByPrimaryKey(empUuid);
	}

	@Transactional
	public void updateByPrimaryKeySelective(Employee employee) {

		employeeMapper.updateByPrimaryKeySelective(employee);
	}

	public boolean checkName(String empUsername) {
		return employeeMapper.checkName(empUsername) == 0 ? false : true;
	}

	public List<Employee> selectAllEmployee(Employee employee) {

		return employeeMapper.selectAllEmployee(employee);
	}

	@Transactional
	public void insert(Employee employee, Integer[] roleUuids) {

		employee.setCreatetime(System.currentTimeMillis());
		employee.setUpdatetime(System.currentTimeMillis());

		// 调用employeeMapper接口的方法插入一条员工数据
		employeeMapper.insertSelective(employee);

		// 判定roleUuids数组是否为空,如果不为空就不插入员工角色的关系
		if (roleUuids != null) {

			// 创建一个list集合，来存储角色id
			List<Integer> list = new ArrayList<Integer>();
			// 创建一个Map集合存取（员工id和角色id)
			Map<String, Object> map = new HashMap<String, Object>();

			// 遍历roleUuids角色数组id
			for (int i = 0; i < roleUuids.length; i++) {
				// 将id添加到list集合中
				list.add(roleUuids[i]);
			}

			// 将（员工id和角色id)存储到map集合中
			map.put("empUuid", employee.getEmpUuid());
			map.put("list", list);

			// 调用employeeMapper接口方法插入员工与角色的关系
			employeeMapper.insertEmpAndRole(map);
		}

	}

	@Transactional
	public void update(Employee employee, Integer empUuid, Integer[] roleUuids) {

		employee.setUpdatetime(System.currentTimeMillis());
		// 修改一条员工数据
		employeeMapper.updateByPrimaryKeySelective(employee);

		// 删除员工与角色之间的关系
		employeeMapper.deleteAllRole(employee.getEmpUuid());

		// 判定角色id数组是否为空
		if (roleUuids != null) {

			// 如果不为空，重新插入员工角色之间的关系
			// 创建一个list集合，来存储角色id
			List<Integer> list = new ArrayList<Integer>();
			// 创建一个Map集合存取（员工id和角色id)
			Map<String, Object> map = new HashMap<String, Object>();

			// 遍历roleUuids角色数组id
			for (int i = 0; i < roleUuids.length; i++) {
				// 将id添加到list集合中
				list.add(roleUuids[i]);
			}

			// 将（员工id和角色id)存储到map集合中
			map.put("empUuid", employee.getEmpUuid());
			map.put("list", list);

			// 调用employeeMapper接口方法插入员工与角色的关系
			employeeMapper.insertEmpAndRole(map);

		}
	}

	@Transactional
	public void delete(Integer empUuid) {

		// 废除一条员工信息
		employeeMapper.deleteEmployeeByEmpUuid(empUuid);
		// 删除员工与角色之间的关系
		employeeMapper.deleteAllRole(empUuid);

	}

	public Employee getEmpUsernameByEmpNameAndEmpIdcard(Employee emp) {

		return employeeMapper.getEmpUsernameByEmpNameAndEmpIdcard(emp);
	}

	public Employee selectEmployeeByUsername(String username) {

		return employeeMapper.selectEmployeeByUsername(username);
	}

	public boolean checkTele(String empTele) {

		return employeeMapper.checkTele(empTele) == 0 ? false : true;
	}

	public List<Employee> selectEmployeeListByDepUuid(Integer depUuid) {

		return employeeMapper.selectEmployeeListByDepUuid(depUuid);
	}

	public void insertEmployeeExcelModelSelective(EmployeeExcelModel emp) {

		employeeMapper.insertEmployeeExcelModelSelective(emp);
	}

}
